package com.liwei.schedule.ScheduleTasker;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ScheduleTasker {
    private int                 corePoolSize = 10;
    ScheduledThreadPoolExecutor scheduler;

    public ScheduleTasker() {
        scheduler = new ScheduledThreadPoolExecutor(corePoolSize);
    }

    public ScheduleTasker(int quantity) {
        corePoolSize = quantity;
        scheduler = new ScheduledThreadPoolExecutor(corePoolSize);
    }

    public void schedule(Runnable event, long delay) {
        scheduler.schedule(event, delay, TimeUnit.SECONDS);
    }

    public void shutdown() {
        scheduler.shutdown();
    }

    public static void main(String[] args) {
        ScheduleTasker tasker = new ScheduleTasker(5);
        tasker.schedule(new Runnable() {
            public void run() {
                System.out.println((System.currentTimeMillis() / 1000) % 1000);
                System.out.println("[1s]Task 1");
            }
        }, 1);
        tasker.schedule(new Runnable() {
            public void run() {
                System.out.println((System.currentTimeMillis() / 1000) % 1000);
                System.out.println("[2s]Task 2");
            }
        }, 2);
        tasker.schedule(new Runnable() {
            public void run() {
                System.out.println((System.currentTimeMillis() / 1000) % 1000);
                System.out.println("[4s]Task 3");
            }
        }, 4);
        tasker.schedule(new Runnable() {
            public void run() {
                System.out.println((System.currentTimeMillis() / 1000) % 1000);
                System.out.println("[10s]Task 4");
            }
        }, 10);
        System.out.println((System.currentTimeMillis() / 1000) % 1000);

        tasker.shutdown();
    }

}